package com.george.controller;

import com.george.entity.Result;
import com.george.entity.StatusCode;
import com.george.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *
 * </p>
 *
 * @author GeorgeChan 2019/4/25 11:28
 * @version 1.0
 * @since jdk1.8
 */
@RestController
@RequestMapping("/api/upload")
public class FileUploadController {
    private final FileUploadService fileUploadService;

    @Autowired
    public FileUploadController(FileUploadService fileUploadService) {
        this.fileUploadService = fileUploadService;
    }

    /**
     * 单个图片上传
     * @param imagefile 图片
     */
    @PostMapping("/single/image")
    public Result uploadSingleImage(@RequestParam("file")MultipartFile imagefile) throws IOException {
        boolean flag = fileUploadService.uoloadSingleImage(imagefile);
        if (flag) {
            return new Result(true, StatusCode.OK, "图片上传成功!");
        }
        return new Result(false, StatusCode.ERROR, "上传失败！");
    }
}
